package com.yingpai.fitness.dvp_2_0.main.activity.mine.integral;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.GsonUtil;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.common.view.XListView;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.address.AddAddressActivity;
import com.yingpai.fitness.dvp_2_0.main.activity.mine.address.AddressSelectActivity;
import com.yingpai.fitness.dvp_2_0.main.bean.integral.IntegralMallSureOrderBean;
import com.yingpai.fitness.dvp_2_0.main.bean.IsSuccessBean;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.ShopDetailMcipChildBean;
import com.yingpai.fitness.entity.address.AddressBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2018/12/19.
 */

public class IntegralMallSureOrderActivity extends CommonActivity {


    @BindView(R.id.add_address)
    LinearLayout addAddress;
    @BindView(R.id.consigneeNameTv)
    TextView consigneeNameTv;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.rl_address)
    LinearLayout rlAddress;
    @BindView(R.id.order_num)
    TextView orderNum;
    @BindView(R.id.ordingMsgLv)
    XListView ordingMsgLv;
    @BindView(R.id.sumPriceTv)
    TextView sumPriceTv;
    @BindView(R.id.freightTv)
    TextView freightTv;
    @BindView(R.id.money)
    TextView money;
    private int purchaseAmount;
    private String merchandiseJson;
    private String merchandiseId;
    private IntegralMallSureOrderBean mIntegralMallSureOrderBean;
    private int addreddId = -1;//地址id
    private QuickAdapter<IntegralMallSureOrderBean.DataBean.ShoppingCartDTOListBean> quickAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.activity_integral_mall_sure_order);
    }

    private void requestData() {
        showDialog(this,null);
        HashMap<String,Object> params = new HashMap();
        params.put("id",merchandiseId);
        params.put("merchandiseJson",merchandiseJson);
        params.put("purchaseAmount",purchaseAmount);
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.EXCHANGE_SHOP,
                params, Url.EXCHANGE_SHOP, new Callback<IntegralMallSureOrderBean>() {
            @Override
            public void onSuccess(IntegralMallSureOrderBean integralMallSureOrderBean) {
                if(integralMallSureOrderBean.getCode().equals(Constant.REQUEST_SUCCESS_0)){
                    mIntegralMallSureOrderBean = integralMallSureOrderBean;
                    if (integralMallSureOrderBean.getData().getAddresses() == null) {
                        consigneeNameTv.setText("");
                        addAddress.setVisibility(View.VISIBLE);
                        rlAddress.setVisibility(View.GONE);
                    } else {
                        addAddress.setVisibility(View.GONE);
                        rlAddress.setVisibility(View.VISIBLE);
                        consigneeNameTv.setText(integralMallSureOrderBean.getData().getAddresses().getReceiverName() + " / " + integralMallSureOrderBean.getData().getAddresses().getReceiverPhoneNo());
                        address.setText(integralMallSureOrderBean.getData().getAddresses().getAddress());
                        addreddId = integralMallSureOrderBean.getData().getAddresses().getId();
                    }

                    orderNum.setText("订单号：" + integralMallSureOrderBean.getData().getOrderCode());

                    Collections.reverse(integralMallSureOrderBean.getData().getPropertySet());

                    quickAdapter.clear();
                    quickAdapter.addAll(integralMallSureOrderBean.getData().getShoppingCartDTOList());
                    quickAdapter.notifyDataSetChanged();

                    sumPriceTv.setText(integralMallSureOrderBean.getData().getPointExchange()+"");
                }else{
                    showToast(integralMallSureOrderBean.getMessage()+"");
                    finishActivity();
                }
                dismissDialog();
            }

            @Override
            public void onError(Response response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void initView() {
        setTitle("确认订单");
        EventBus.getDefault().register(this);

        Bundle bundle = getIntent().getExtras();
        if(bundle!=null){
            merchandiseId = bundle.getString("merchandiseId");
            merchandiseJson = bundle.getString("merchandiseJson");
            purchaseAmount = bundle.getInt("purchaseAmount",-1);
        }

        quickAdapter = new QuickAdapter<IntegralMallSureOrderBean.DataBean.ShoppingCartDTOListBean>(this, R.layout.item_ready_order) {
            @Override
            protected void convert(BaseAdapterHelper helper, IntegralMallSureOrderBean.DataBean.ShoppingCartDTOListBean item) {
                helper.setText(R.id.title, item.getMerchandiseName()+"");
                ImageView photo = helper.getView(R.id.photo);
                Glide.with(context)
                        .load(item.getPhotoUrl())
                        .placeholder(R.mipmap.default_big_bg)
                        .into(photo);

                List<ShopDetailMcipChildBean> propertyList = GsonUtil.jsonToList(item.getPropertyValue(), ShopDetailMcipChildBean.class);
                String s = "";
                for (int i = 0; i < propertyList.size(); i++) {
                    if (i == propertyList.size() - 1) {
                        s = s + propertyList.get(i).getPropertyValue();
                    } else {
                        s = s + propertyList.get(i).getPropertyValue() + "；";
                    }
                }

                helper.setText(R.id.count, "数量：X" + item.getQuantity() + "；" + s);
            }
        };
        ordingMsgLv.setAdapter(quickAdapter);
    }

    @Override
    protected void initData() {
        requestData();
    }


    @Subscribe(threadMode = ThreadMode.POSTING)
    public void OnMessageEvent(AddressBean.MapBean.AddressesBean bean) {
        addAddress.setVisibility(View.GONE);
        rlAddress.setVisibility(View.VISIBLE);
        consigneeNameTv.setText(bean.getReceiverName() + " / " + bean.getReceiverPhoneNo());
        address.setText(bean.getAddress());
        addreddId = bean.getId();
    }

    @Override
    @OnClick({R.id.add_address, R.id.rl_address, R.id.ordingOkBtn})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.add_address:
                startActivity(AddAddressActivity.class);
                break;
            case R.id.rl_address:
                startActivity(AddressSelectActivity.class);
                break;
            case R.id.ordingOkBtn:
                if(addreddId!=-1){
                    showDialog(this,null);
                    HashMap<String,Object> map = new HashMap<>();
                    map.put("pmoid",mIntegralMallSureOrderBean.getData().getPointMallOrderId());
                    map.put("receiveAddr",addreddId);
                    finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.EXCHANGE_SHOP_SURE, map, Url.EXCHANGE_SHOP_SURE, new Callback<IsSuccessBean>() {
                        @Override
                        public void onSuccess(IsSuccessBean isSuccessBean) {
                            dismissDialog();
                            if(isSuccessBean.getCode().equals(Constant.REQUEST_SUCCESS_0)){
                                EventBus.getDefault().post("==exchange==");
                                finishActivity();
                            }else{
                                showToast(isSuccessBean.getMessage()+"");
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            dismissDialog();
                        }
                    });
                }else{
                    showToast("请填写收货地址");
                }

                break;
        }
    }
}
